package cn.huijielong.modules.group.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.Page;

import cn.huijielong.modules.sys.controller.AbstractController;
import cn.huijielong.modules.group.entity.GoodsEntity;
import cn.huijielong.modules.group.service.GoodsService;
import cn.huijielong.common.utils.PageUtils;
import cn.huijielong.common.utils.Query;
import cn.huijielong.common.utils.RD;




/**
 * 商品
 * 
 * @author xys
 * @email xys@gmail.com
 * @date 2019-07-03 22:46:43
 */
@RestController
@RequestMapping("/group/goods")
public class GoodsController  extends AbstractController{
	@Autowired
	private GoodsService goodsService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("group:goods:list")
	public RD list(@RequestParam Map<String, Object> params){
	    logger.info("从{}发送请求，参数：{}",getHeader("referer"),params.toString());
		//查询列表数据
        Query query = Query.createRequest(params);
		Page<GoodsEntity> goodsList = goodsService.queryList(query);		
		PageUtils pageUtil = new PageUtils(goodsList, goodsList.getTotal(), query.getLimit(), query.getPage());
		
		return RD.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{goodsId}")
	@RequiresPermissions("group:goods:info")
	public RD info(@PathVariable("goodsId") Long goodsId){
		GoodsEntity goods = goodsService.queryObject(goodsId);
		
		return RD.ok().put("goods", goods);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("group:goods:save")
	public RD save(@RequestBody GoodsEntity goods){
		goodsService.save(goods);
		
		return RD.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("group:goods:update")
	public RD update(@RequestBody GoodsEntity goods){
		goodsService.update(goods);
		
		return RD.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("group:goods:delete")
	public RD delete(@RequestBody Long[] goodsIds){
		goodsService.deleteBatch(goodsIds);
		
		return RD.ok();
	}
	
}
